<?php

class LoginController extends Zend_Controller_Action
{
    protected $_session;

    public function init()
    {
        //if (Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->getNoRender()){
        //	echo 'ppp';
        //}
        $this->_session = new Zend_Session_Namespace(__CLASS__);
        //        $this->view->headTitle('學務系統登入檢查') ;

        if ($this->getRequest()->getParam('controller')<>'login')
        $this->view->request_url = $this->getRequest()->getParam('module').'/'.$this->getRequest()->getParam('controller').'/'.$this->getRequest()->getParam('action');
        //$this->setMenu();
    }

    /**
     * 判斷是否為 ajax 型態
     */
    function is_xhr() {
        return @ $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest';
    }


    public function indexAction()
    {
        if ($this->is_xhr())
        $this->_forward('relogin');
        //        if ($this->getRequest()->getParam('relogin'))
        //            $this->view->relogin = 1;
        $form = new Default_Form_Login();
        $this->view->form = $form;
        $request = $this->getRequest();
        if ($request->isPost()) {
            if ($form->isValid($request->getParams())) {
                $username = $form->getValue('username');
                $password = $this->_helper->PassOperate($form->getValue('password'));
                //$password = md5($form->getValue('password'));

                $adapter = new Sfs_Auth_Adapter($username, $password);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($adapter);
                $auth->setStorage(new Zend_Auth_Storage_Session('Sfs'));

                $logger = Zend_Registry::get('logger');
                if ($result->isValid()) {
                    // success : store database row to auth's storage system
                    // (not the password though!)
                    //$data = $authAdapter->getResultRowObject(null ,'password');
                    $data = $result->getIdentity();
                    $auth->getStorage()->write($data);
                    // 記錄登入時間
                    $q = Doctrine_Query::Create()
                    ->update('Default_Model_Users')
                    ->set('lastvisitDate','?',date("Y-m-d H:i:s"))
                    ->where('id=?', $data->id);
                    $q->execute();

                    $logger->info($username." logon from ".$_SERVER['REMOTE_ADDR']);

                    //$this->_forward('redirect');

                    if ($this->_getParam('request_url'))
                    $this->_redirect('/'.$this->_getParam('request_url'));
                    else
                    $this->_redirect('/');
                } else {
                    // failure: clear database row from session
                    $logger->info($username." logon failure from ".$_SERVER['REMOTE_ADDR']);
                    $this->view->message = 'Login failure';
                    $this->view->assign($this->_getParam('username'));
                }
            }
        }

    }

    public  function noaclAction()
    {

    }
    public function reloginAction()
    {
        $this->getHelper('layout')->disableLayout();
    }
}
